
//选中的设备号输入框
var searchtextObj;
var searchtouchObj;
var orgstrapvclScrollTop;

$(function(){
	//缓存
	var cache = {};
	//设备号输入提示（高级查询按钮前Input）
	var searchtext = $("#VehicleOperLine");
	var searchbtn = $("#VehicleOperLineBtn");
	//提示框属性
	var searchtextleft;
	var searchtexttop;
	var searchtextwidth;
	var searchtextheight;
	
	//显示提示框，obj：触发操作的目前目标对象
	var isAjax = true;
	var displayTip = function(obj,url){
		$(obj).keyup(function(e){
			//显示框
			var searchsel = $("#searchOperLine");
			//设置属性
			searchtextleft =  searchtext.offset().left;
			searchtexttop = searchtext.offset().top+searchtext.height()+2;
			searchtextwidth = searchtext.width()+2;
			var offset = 15;
			//设置提示框对象
			searchtextObj = $(obj);
			//过滤操作
			if(e.keyCode==13)return ;
			var searchVcl = $(obj).val();
			if(searchVcl==''){
				searchsel.remove();
				return ;
			}
			//清除提示框
			if(searchsel.length>0)searchsel.remove();
			if(searchsel.length>0)return ;
			
			var callBack = function(data){
				eval('var list = '+data);
				var objs = list.data;
				if(objs.length==0){
					return ;
				}else{
					var select$ = $("<select>")
					.attr("id","searchOperLine")
					.css("position","absolute")
					.css("left",searchtextleft)
					.css("top",searchtexttop)
					.css("width",searchtextwidth);
			//		.addClass("searchsel"+type);
					var counts = 0;
					if((navigator.userAgent.match(/iPad/i))){
						select$.get(0).options.add(new Option('',''));
					}
					$.each(objs,function(i,obj){
						counts++;
						var option = new Option(obj.operlinename,obj.operLineId);
						select$.get(0).options.add(option);
					});
					searchtextheight=30*counts-offset*(counts-1);
					select$.get(0).options[0].selected = "selected";
					//进行非iPad单独设置
					if(!(navigator.userAgent.match(/iPad/i))){
						select$.attr("multiple","multiple");
						select$.height(searchtextheight);
					}
					select$.appendTo($("body"));
					//进行iPad单独设置
					if((navigator.userAgent.match(/iPad/i))){
						select$.click(function(event){event.stopPropagation();});
						select$.change(function(){
							$(obj).val(select$.val());
							select$.remove();
						});
					}else{
						bindSelClick1();
						enterClick1();
					}
				}
			};
			if ( searchVcl in cache ) {
				callBack(cache[searchVcl]);
				return ;
			}
			if(isAjax){
				isAjax = false;
				$.ajax({
					url:url,
					data:{'operLine':searchVcl},
					dataType:'text',
					type:'post',
					success:function(data){
						cache[searchVcl] = data;
						callBack(data);
						isAjax = true;
					}
				});
			}
		});
	};
	
	//加载设备号输入提示（高级查询按钮前Input）
	if(searchtext!=undefined&&searchtext.length>0){
		searchtext.each(function(){
			displayTip($(this),'selbase!getAccount_VehicleOperLine.action');
		});
		var operLine = "";
		
		
		//设置属性
		searchtextleft =  searchtext.offset().left;
		searchtexttop = searchtext.offset().top+searchtext.height()+2;
		searchtextwidth = searchtext.width()+2;
		
		
		searchtext.click(function(){
			if(searchtext.val().length != 0){
				return;
			}
			$.ajax({
				url:'selbase!getAccount_VehicleOperLine.action',
				data:{'operLine':operLine},
				dataType:'text',
				type:'post',
				success:function(data){

					eval('var list = '+data);
					var objs = list.data;
					if(objs.length==0){
						return ;
					}else{
						var select$ = $("<select>")
						.attr("id","searchOperLine")
						.css("position","absolute")
						.css("left",searchtextleft)
						.css("top",searchtexttop)
						.css("width",searchtextwidth);
				//		.addClass("searchsel"+type);
						var counts = 0;
						if((navigator.userAgent.match(/iPad/i))){
							select$.get(0).options.add(new Option('',''));
						}
						$.each(objs,function(i,obj){
							counts++;
							var option = new Option(obj.operlinename,obj.operLineId);
							select$.get(0).options.add(option);
						});
						var offset = 15;
						searchtextheight=30*counts-offset*(counts-1);
						select$.get(0).options[0].selected = "selected";
						//进行非iPad单独设置
						if(!(navigator.userAgent.match(/iPad/i))){
							select$.attr("multiple","multiple");
							select$.height(searchtextheight);
						}
						select$.appendTo($("body"));
						//进行iPad单独设置
						if((navigator.userAgent.match(/iPad/i))){
							select$.click(function(event){event.stopPropagation();});
							select$.change(function(){
								$(obj).val(select$.val());
								select$.remove();
							});
						}else{
							bindSelClick1();
							enterClick1();
						}
					}
				}
			});
		});
	}
	
	//页面滚动条滚动
//	$(".bodyWidth").scroll(function(){
//		if($(".orgstrapvcl").length>0){
//			searchtextObj = $(".orgstrapvcl");
//			if($('#orgnostrapvcl').length>0){
//				searchtouchObj = $(".selOrg");
//			}else if($('#orgstrapvcl').length>0){
//				searchtouchObj = $(".searchtext");
//			}
//		}else if($("#searchsel").length>0){
//			searchtextObj = $("#searchsel");
//			if($(".searchsel1").length>0){
//				searchtouchObj = $(".searchtext");
//			}else if($(".searchsel2").length>0){
//				searchtouchObj = $(".searchvcl");
//			}else if($(".searchsel3").length>0){
//				searchtouchObj = $(".searchvcltype");
//			}
//		}
//		if(searchtextObj!=null&&searchtextObj!=undefined){
//			searchtextleft =  searchtouchObj.offset().left;
//			searchtexttop = searchtouchObj.offset().top+searchtouchObj.height();
//			searchtextObj.css("left",searchtextleft);
//			searchtextObj.css("top",searchtexttop+2);
//		}
//	
//	});
	
	//点击页面
	$(document).click(function(event){
		if($("#searchOperLine").length>0)$("#searchOperLine").remove();
	});
	
});

function bindSelClick1(){
	$("#searchOperLine").unbind("click");
	$("#searchOperLine").click(function(){
		selClick1($(this));
	});
}

function enterClick1(){
	$(document).unbind("keydown");
	$(document).keydown(function(e){
		if(e.keyCode==13){
			if($("#searchOperLine").length==0)return ;
			selClick1($("#searchOperLine"));
			return false;
		}else if(e.keyCode==38||e.keyCode==40){
			if($("#searchOperLine").length>0)$("#searchOperLine").focus();
		}
	});
}


function selClick1(obj){
	$("#VehicleOperLine").focus();
	if($(obj).length<=0||obj==undefined)return ;
	var index = $(obj).get(0).selectedIndex;
	$("#VehicleOperLine").val($(obj).get(0).options[index].value);
	$(obj).remove();
};